package com.uinnova.product.eam.mvc;

import com.alibaba.fastjson.JSONObject;
import com.binary.core.exception.BinaryException;
import com.binary.core.util.BinaryUtils;
import com.binary.framework.web.RemoteResult;
import com.binary.jdbc.Page;
import com.uinnova.product.eam.model.es.AssetQueryInfo;
import com.uinnova.product.eam.model.es.DiagramJsonQueryInfo;
import com.uinnova.product.eam.model.es.ProjectInfo;
import com.uinnova.product.eam.service.InterfaceService;
import com.uinnova.product.vmdb.comm.doc.annotation.ModDesc;
import com.uino.bean.cmdb.base.ESCIInfo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 对外接口
 */
@RestController
@RequestMapping("/interface")
public class InterfaceController {

    @Resource
    private InterfaceService interfaceService;

    /**
     * 只能PaaS平台调用
     * @param request
     * @param assetQueryInfo
     * @return
     */
    @PostMapping("/assetList")
    @ModDesc(desc = "根据分类名称查询对应的资产列表数据", pDesc = "", rDesc = "", rType = RemoteResult.class)
    public RemoteResult assetList(HttpServletRequest request, @RequestBody AssetQueryInfo assetQueryInfo) {
        String apiKey = request.getHeader("apiKey");
        assetQueryInfo.setApiKey(apiKey);
        BinaryUtils.checkEmpty(assetQueryInfo.getSysName(), "系统名称");
        BinaryUtils.checkEmpty(assetQueryInfo.getApiKey(), "apiKey");
        BinaryUtils.checkEmpty(assetQueryInfo.getClassName(), "分类名称");
        BinaryUtils.checkEmpty(assetQueryInfo.getPageNum(), "页码");
        BinaryUtils.checkEmpty(assetQueryInfo.getPageSize(), "每页数");
        Page<ESCIInfo> result = interfaceService.assetList(assetQueryInfo);
        return new RemoteResult(result);
    }

    /**
     * 只能PaaS平台调用
     * @param request
     * @param diagramJsonQueryInfo
     * @return
     */
    @PostMapping("/diagramJson")
    @ModDesc(desc = "视图json查询", pDesc = "", rDesc = "", rType = RemoteResult.class)
    public RemoteResult diagramJson(HttpServletRequest request, @RequestBody DiagramJsonQueryInfo diagramJsonQueryInfo) {
        String apiKey = request.getHeader("apiKey");
        diagramJsonQueryInfo.setApiKey(apiKey);
        JSONObject result = interfaceService.diagramJson(diagramJsonQueryInfo);
        return new RemoteResult(result);
    }

    /**
     * 只能RDMP平台调用
     * @param request
     * @param projectInfo
     * @return
     */
    @PostMapping("/projectSave")
    @ModDesc(desc = "项目数据保存", pDesc = "", rDesc = "", rType = RemoteResult.class)
    public RemoteResult projectSave(HttpServletRequest request, @RequestBody ProjectInfo projectInfo) {
        String apiKey = request.getHeader("apiKey");
        projectInfo.setApiKey(apiKey);
        BinaryUtils.checkEmpty(projectInfo.getSysName(), "系统名称");
        BinaryUtils.checkEmpty(projectInfo.getApiKey(), "apiKey");
        BinaryUtils.checkEmpty(projectInfo.getClassName(), "分类名称");
        BinaryUtils.checkEmpty(projectInfo.getProjectName(), "项目名称");
        BinaryUtils.checkEmpty(projectInfo.getPreReviewNum(), "预审编号");
        BinaryUtils.checkEmpty(projectInfo.getProjectOwner(), "项目负责人");
        Long result = interfaceService.projectSave(projectInfo);
        return new RemoteResult(result);
    }

    /**
     * 只能RDMP平台调用
     * @param request
     * @param projectInfo
     * @return
     */
    @PostMapping("/projectQuery")
    @ModDesc(desc = "项目数据查询", pDesc = "", rDesc = "", rType = RemoteResult.class)
    public RemoteResult projectQuery(HttpServletRequest request, @RequestBody ProjectInfo projectInfo) {
        String apiKey = request.getHeader("apiKey");
        projectInfo.setApiKey(apiKey);
        BinaryUtils.checkEmpty(projectInfo.getSysName(), "系统名称");
        BinaryUtils.checkEmpty(projectInfo.getApiKey(), "apiKey");
        BinaryUtils.checkEmpty(projectInfo.getClassName(), "分类名称");
        if (BinaryUtils.isEmpty(projectInfo.getPreReviewNum()) && BinaryUtils.isEmpty(projectInfo.getProjectOwner())) {
            throw new BinaryException("预审编号和项目负责人 不能同时为空！");
        }
        List<ProjectInfo> result = interfaceService.projectQuery(projectInfo);
        return new RemoteResult(result);
    }

    /**
     * 已发布方案的历史数据处理
     * 1、已发布的方案 根据时间相近原则匹配对应的发布流程，生成流程与发布版本的关联关系
     * 2、已发布的方案根据对应的流程查询当时的批注和问题列表，生成对应版本的批注和问题数据
     * @return
     */
    @PostMapping("/planHistoryData")
    @ModDesc(desc = "已发布方案的历史数据处理", pDesc = "", rDesc = "", rType = RemoteResult.class)
    public RemoteResult planHistoryData() {
        interfaceService.planHistoryData();
        return new RemoteResult("");
    }

    /**
     * 方案审批流程历史数据处理
     * 建设模式（buildMode）字段更新
     * DEV_SELF 更新为 自主研发
     * COOPERATIVE 更新为
     * @return
     */
    @PostMapping("/dealArchReviewInfo")
    @ModDesc(desc = "方案审批流程历史数据处理", pDesc = "", rDesc = "", rType = RemoteResult.class)
    public RemoteResult dealArchReviewInfo() {
        interfaceService.dealArchReviewInfo();
        return new RemoteResult("");
    }
}
